using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_usercp_pm_newmessage : Controls.DataManagerUserControl
{
    public delegate void OnMessageCreatedDelegate(object sender, DAL.PrivateMessage message);
    public event OnMessageCreatedDelegate MessageCreated;

    protected override void ItemChanged()
    {
        if (CurrentItem != null)
        {
            mviewMsg.SetActiveView(viewMsg);
            ClearFields();
            Page.Title = "FSTDT Forums: " + "Send New Private Message";
        }
        else
        {
            mviewMsg.SetActiveView(viewError);
            Page.Title = "FSTDT Forums: " + "Oops!";
        }
    }

    public void SetRecipient(DAL.User recipient)
    {
        ClearFields();
        if (recipient != null)
        {
            txtTo.Text = recipient.Username;
        }
    }

    public void QuoteMessage(DAL.PrivateMessage msg)
    {
        ClearFields();
        if (msg != null)
        {
            txtTo.Text = msg.From.Username;
            if (!msg.Subject.StartsWith("RE: "))
            {
                txtSubject.Text = string.Format("RE: {0}", msg.Subject);
            }
            else
            {
                txtSubject.Text = msg.Subject;
            }

            txtMessage.Text = string.Format("[quote={0}]{1}[/quote]", msg.From.Username, msg.Message);
        }
    }

    public void ReplyToMessage(DAL.PrivateMessage msg)
    {
        QuoteMessage(msg);
        txtMessage.Text = "";
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (DAL.Users.GetByUsername(args.Value) != null)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && CurrentUser != null)
        {
            DAL.PrivateMessage myPM = new DAL.PrivateMessage();
            myPM.To = DAL.Users.GetByUsername(txtTo.Text);
            myPM.From = CurrentUser;
            myPM.Subject = txtSubject.Text;
            myPM.Message = txtMessage.Text;

            DAL.PrivateMessages.Add(myPM);

            ClearFields();

            if (MessageCreated != null)
            {
                MessageCreated(this, myPM);
            }
        }
    }

    private void ClearFields()
    {
        txtTo.Text = "";
        txtSubject.Text = "";
        txtMessage.Text = "";
    }
    protected void btnPreview_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            PreviewPost1.SetPreview(txtSubject.Text, txtMessage.Text);
        }
    }
}
